package com.work.controller;

import com.github.pagehelper.PageInfo;
import com.work.pojo.Goods;
import com.work.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 产品控制器
 *
 * @author gy
 * &#064;date 2022/09/29
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/getGoodsListByTypeId")
    public ModelAndView getGoodsListByTypeId(String typeId, @RequestParam(defaultValue = "2") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
        int parseInt = Integer.parseInt( typeId );
        PageInfo goodsListByTypeId = goodsService.getGoodsListByTypeId( parseInt, pageNum, pageSize );
        ModelAndView mv = new ModelAndView();
        mv.addObject( "pageBean", goodsListByTypeId );
        mv.addObject( "typeId", typeId );
        mv.setViewName( "forward:/goodsList.jsp" );
        return mv;
    }

    @PostMapping("/addGoods")
    public ModelAndView addGoods(Goods goods, ModelAndView modelAndView) {
        goodsService.addGoods( goods );
        modelAndView.setViewName( "forward:/admin/showGoods.jsp" );
        List<Goods> goodsList = goodsService.getGoodsList();
        modelAndView.addObject( "goodsList", goodsList );
        return modelAndView;
    }

    @RequestMapping("/listGoods")
    public ModelAndView listGoods(ModelAndView modelAndView) {
        List<Goods> goodsList = goodsService.getGoodsList();
        modelAndView.setViewName( "forward:/admin/showGoods.jsp" );
        modelAndView.addObject( "goodsList", goodsList );
        return modelAndView;
    }

    @RequestMapping("/goodstypelist")
    public ModelAndView goodstypelist(ModelAndView modelAndView) {
        List<Goods> goodsList = goodsService.getGoodsList();
        modelAndView.setViewName( "forward:/admin/showGoods.jsp" );
        modelAndView.addObject( "goodsList", goodsList );
        return modelAndView;
    }

    @RequestMapping("/getGoodsById")
    public ModelAndView getGoodsById(ModelAndView modelAndView, Integer id) {
        Goods goods = goodsService.getGoods( id );
        modelAndView.setViewName( "forward:/goodsDetail.jsp" );
        modelAndView.addObject( "goods", goods );
        return modelAndView;
    }

}
